
// pages/index/component/travelList/travelList.js
import { list, getArea } from '../../../../api/scenicSpot'
const util = require('../../../../utils/util')
const app=getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  lifetimes: {
    attached() {
      // 设定为当前所在市代码 ，未启用
      // this.setData({
      //   currentAreaId: wx.getStorageSync('currentAreaId')
      // })
      console.log(app.globalData.areaId)
      setTimeout(()=>{
        this.getAreas()
      },1000)
  
      // this.loadList()
    }
  },
  pageLifetimes:{
    ready(){
  
      this.triggerEvent('getCanHeight')
    },
    // show(){
    //   this.triggerEvent('getLvyouHeight')
    // },
  },
  /**
   * 组件的初始数据
   */
  data: {
    travles: [],
    pageNum:1,
    isIndex: 0,
    title: [],
    currentAreaId: 350300
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onReachBottom(){
      this.data.pageNum+=1
      if(this.data.isIndex==0){
        this.loadList('')
      }else{
        this.loadList(this.data.title[this.data.isIndex].id)
      }
     
    },
    downRefresh(){
      this.data.pageNum=1
      this.data.travles=[]
      this.getAreas()
    },
    getAreas() {
      let _currentAreaId = app.globalData.areaId
      getArea(350300).then(res => {
        this.setData({
          title: [{ name: "全部", id: '' }, ...res.data]
        })
          this.loadList(this.data.title[0].id)
      })
    },
    loadList(data) {
      list({
         areaId: data, 
         lat: app.globalData.lat, 
         lng: app.globalData.lng,
         pageNum:this.data.pageNum 
        }).then(res => {
        res.data.data.forEach(item=>{
          item.distance=util.rateConverter(item.distance * 1000)
          item.thumbnail=util.thumbnail(item.thumbnail, 172, 166)
          if(util.isNull(item.love)){
            item.love=0
          }
          item.number=[]
       
          if(!util.isNull(item.score)){
            item.score=item.score.toFixed(0)
          }else{
            item.score=0
            // item.score=item.score.toFixed(1)
          }
          for(var i=0;i<5;i++){
            if(i<item.score){
              item.number.push('https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/ic_list_star_red%402x.png')
            }else{
              item.number.push('http://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/dingdan/bddd9200c54aea2f8e84ca2b75a0ee8.png')
            }
           
          }
          this.data.travles.push(item)
        })
        this.triggerEvent('getLvyouHeight')
        this.setData({
          travles:this.data.travles,
        })
      })
    },
    toTitle(data) {
      this.data.isIndex = data.currentTarget.dataset.index
      this.data.pageNum=1
      this.data.travles=[]
      this.setData({
        isIndex: this.data.isIndex,
        scrollLeft: (data.currentTarget.dataset.index - 1) * 130
      })
      this.loadList(this.data.title[this.data.isIndex].id)
      this.triggerEvent('getLvyouHeight')
    },


    travellistClick(e) {
      wx.navigateTo({
        url: `/pages/travel/travel/index?scenicSpotId=${e.currentTarget.dataset.scenicSpotId}`,
      })
    }
  }
})
